#include<stdio.h>
#include<stdlib.h>
int count=0;

// Creating an array

int create(int *p)
{
int i;

for(i=0;i<100;i++)
 {printf("Enter a number (0 to terminate)");
  scanf("%d",(p+i));
   count++;
 if(*(p+i)==0)
break;

}
for(i=0;i<=count-1;i++)
printf("%d\n",*(p+i));


}

//Insert function

int insert(int*p,int k,int x)
{int i;
for(i=count-1;i>=k-1;i--)
*(p+(i+1))=*(p+i);
*(p+k)=x;

count=count+1;
for(i=0;;i++)
{printf("%d\n",*(p+i));
if(*(p+i)==0)
break;}
printf("\n");
}

//Reverse function

int reverse( int *p)
{int i,x,j;
for(i=0,j=count-1;i<count/2;i++,j--)
{
 x=*(p+i);
 *(p+i)=*(p+j);
 *(p+j)=x;
}
for(i=0;i<=count-1;i++)
printf("%d\n",*(p+i));
printf("\n");
}

//Append function

int append(int *p,int m)
{int i;
*(p+count)=m;
count=count+1;
for(i=0;i<=count-1;i++)
printf("%d\n",*(p+i));

}

// Count function

int Count(int*p,int m)
{int i,c=0;
for(i=0;i<count;i++)
{ 
  if (*(p+i)!=m)
  continue;
  else 
  c=c+1;
}

printf("Number of times %d,occur==%d",m,c);

}

// Index function

int ind(int*p,int m)
{int i,flag=0;
for(i=0;i<count;i++)
{if(*(p+i)==m)
 {flag=1;
 break;}
}
 if(flag==1)
return i;
else
printf("No such element exits");
}

//Pop function

int pop(int *p,int k)
{int i,s;
for(i=k;i<count-1;i++)
*(p+i)=*(p+i+1);
s=4*(count-1);
p=realloc(p,s);
count=count-1;
for(i=0;i<=count-1;i++)
printf("%d\n",*(p+i));
printf("\n");
}

//Extend function

int extend(int*p)
{int *q;
int i,j,n;
printf("Enter no. of elements to be added");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{printf("Enter element of list to be added");
scanf("%d",&a[i]);
}
q=a;


for(i=count,j=0;j<n;i++,j++)
*(p+i)=*(q+j);
count=count+n;
for(i=0;i<=count-1;i++)
printf("%d\n",*(p+i));
printf("\n");
}

//Remove function

int rem(int *p,int m)
{
int i,j;
int s;
j=ind(p,m);
if(j>=0)
{for(i=j;i<count-1;i++)
*(p+i)=*(p+i+1);
s=4*(count-1);
p=realloc(p,s);
count=count-1;
for(i=0;i<=count-1;i++)
printf("%d\n",*(p+i));
printf("\n");
}
else
printf("Element is not in the list");
}

//Sort function

int sort(int *p)
{
int i,j,tmp;
for(i=0;i<count;i++)
{for(j=0;j<(count-1)-i;j++)
 {
   if(*(p+j)>*(p+j+1))
     {
       tmp=*(p+j);
       *(p+j)=*(p+j+1);
         *(p+j+1)=tmp;}
}
}


for(i=0;i<=count-1;i++)
printf("%d\n",*(p+i));
printf("\n");
}
int function(int *p,int ch)
{int c=1;
int D;
switch(ch)
{
case 1:while(c)
           {int m,k;
            printf("Enter the element to insert and the index");
            scanf("%d,%d",&m,&k);
            insert(p,k,m);
            printf("Press Y to enter more elements ,N to exit");
            scanf("%d",&c);
            }
            

case 2 : while(c)
           {int m,k;
            printf("Enter the element to append");
            scanf("%d",&m);
            append(p,m);
            printf("Press 1 to append more elements ,0 to exit");
            scanf("%d",&c);
            }
            break;

case 3 :    reverse(p);
            break;
case 4 :while(c)
           {int m,k;
            printf("Enter the index");
             scanf("%d",&k);
             pop(p,k);
             printf("Press 1 to pop more elements ,0 to exit");
             scanf("%d",&c);
            }
            break;

case 5 : sort(p);
         break;
case 6 :while(c)
           {int m,k;
            printf("Enter the element to delete");
            scanf("%d",&m);
            rem(p,m);
            printf("Press 1 to delete more elements ,0 to exit");
            scanf("%d",&c);
            }

           break;
case 7 : while(c)
           {
            extend(p);
            printf("Press 1 to extend more elements ,0 to exit");
            scanf("%d",&c);
            }

           break; 
case 8: while(c)
           {int m,k;
            printf("Enter the element to count");
            scanf("%d",&m);
            Count(p,m);
            printf("Press 1 to count more elements ,0 to exit");
            scanf("%d",&c);
            }
             break; 
case 9: while(c)
           {int m,k;
            printf("Enter the element to find index");
            scanf("%d",&m);
            ind(p,m);
            printf("Press 1 to find index of more elements ,0 to exit");
            scanf("%d",&c);
            }

            
}

printf("Want to enter more functions Press 1 , 0 to exit");
scanf("%d",&D);
if(D)
return 1;
else
return 0;
}




int main()
{int i,z,t;
int *p;
int C=1;
p=malloc(100);
create(p);
while(C)
{printf("\n FUNCTIONS:");

                printf("\n 1.Insert");

                printf("\n 2.Append");

                printf("\n 3.Reverse");

                printf("\n 4.Pop");

                printf("\n 5.Sort");

                printf("\n 6.Remove");

                printf("\n 7.Extend");

                printf("\n 8.Count");

                printf("\n 9.Index");

                


printf("\nEnter function");
scanf("%d",&t);
z=function(p,t);
if(z)
C=1;
else C=0;
}

}